package com.etc.servlet;

import com.etc.dao.BookDao;
import com.etc.entity.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author kalista
 * @Description
 * @Date 2020/12/11  9:55
 **/
@WebServlet("/success")
public class SuccessServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BookDao bookDao = new BookDao();
        try {
            List<Book> books = bookDao.getBooks();
            StringBuffer stringBuffer = new StringBuffer();
            resp.setCharacterEncoding("UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<!DOCTYPE html>");
            writer.println("<html lang=\"en\">");
            writer.println("<head>");
            writer.println("<meta charset=\"UTF-8\">");
            writer.println("<title>Title</title>");
            writer.println("</head>");
            writer.println("<body>");
            writer.println("<h2>" + "登陆成功" +"</h2>");
            writer.println("<table><tr><td>id</td><td>name</td><td>操作</td></tr>");

            for (Book book : books) {
                stringBuffer.append("<tr>")
                        .append("<td>")
                        .append(book.getId())
                        .append("</td>")
                        .append("<td>")
                        .append(book.getName())
                        .append("</td>").append("</tr>").append("<td>")
                        .append("<a href=\"#\">" +"删除"+"</a>")
                        .append("</td>");
            }

            writer.println(stringBuffer.toString());
            writer.println("</table>");

            writer.println("</body>");
            writer.println("</html>");

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
